<template>
  <div>
    <div class="mb-[10px]">
      <el-card>
        <el-form
          ref="queryFormRef"
          :model="queryParams"
          :inline="true"
          label-width="auto"
        >
          <el-form-item
            label="创建时间"
          >
            <el-date-picker
              v-model="dateRange"
              value-format="YYYY-MM-DD"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width: 240px"
            ></el-date-picker>
          </el-form-item>
          <el-form-item
            label="企业名称"
            prop="opName"
          >
            <el-input
              v-model="queryParams.opName"
              placeholder="请输入企业名称"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item
            label="客户名称"
            prop="workName"
          >
            <el-input
              v-model="queryParams.workName"
              placeholder="请输入客户名称"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item
            label="身份证号"
            prop="workIdCard"
          >
            <el-input
              v-model="queryParams.workIdCard"
              placeholder="请输入身份证号"
              clearable
              style="width: 240px"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item
            label="签约状态"
            prop="signStatus"
          >
            <el-select
              v-model="queryParams.signStatus"
              placeholder="请选择签约状态"
              clearable
              filterable
              style="width: 240px"
            >
              <el-option
                v-for="(item,i) in sign_status"
                :label="item.label"
                :value="item.value"
                :key="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="handleQuery"
              icon="Search"
            >查询</el-button>
            <el-button
              @click="resetQuery"
              icon="Refresh"
            >重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <el-card>
      <el-table
        v-loading="loading"
        :data="contractInquiryList"
        border
      >
        <el-table-column
          label="创建时间"
          align="center"
          min-width="160"
          prop="createTime"
          sortable
        />

        <el-table-column
          label="企业名称"
          align="center"
          :show-overflow-tooltip="true"
          min-width="160"
          prop="opName"
        />
        <el-table-column
          label="客户名"
          align="center"
          min-width="160"
          prop="workName"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="身份证号"
          align="center"
          min-width="160"
          prop="cardId"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="签约状态"
          align="center"
          min-width="160"
          prop="signStatus"
        >
          <template #default="scope">
            <dict-tag
              :options="sign_status"
              :value="scope.row.signStatus"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          fixed="right"
          align="center"
          width="120"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-tag
              link
              type="primary"
              @click="lookDetail(scope.row)"
            >详情</el-tag>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 签约详细信息 -->
    <el-dialog
      v-model="dialogVisible"
      title="签约详细信息"
      width="70%"
    >
      <div class="row">
        <div class="col">
          <div class="label">
            用工姓名：
          </div>
          <div class="value"> {{ detailForm.workName||'' }} </div>
        </div>
        <div class="col">
          <div class="label">
            身份证号：
          </div>
          <div class="value"> {{ detailForm.cardId||'' }} </div>
        </div>
        <div class="col">
          <div class="label">
            企业名称：
          </div>
          <div class="value"> {{ detailForm.opName||'' }} </div>
        </div>
        <div class="col">
          <div class="label">
            合同编号：
          </div>
          <div class="value"> {{ detailForm.no||'' }} </div>
        </div>
        <div class="col">
          <div class="label">
            签约文档地址：
          </div>
          <div class="value">
            <a
              v-if="detailForm.signFileUrl"
              :href="detailForm.signFileUrl"
            >下载签约文档</a>
          </div>
        </div>
        <div class="col">
          <div class="label">
            签约文档在线地址：
          </div>
          <div class="value">
            <a
              v-if="detailForm.signFileUrl"
              :href="detailForm.signFileUrl"
              target="blank"
            >查看签约文档</a>
          </div>
        </div>
        <div class="col">
          <div class="label">
            签约状态：
          </div>
          <div class="value">
            <dict-tag
              :options="sign_status"
              :value="detailForm.signStatus"
            />
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="signTable" lang="ts">
import api from '@/api/workMag/contractInquiry';
import { ContractInquiryQuery, ContractInquiryVO, AuthDetailVo } from '@/api/workMag/contractInquiry/types';

const router = useRouter();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const { sign_status } = toRefs<any>(proxy?.useDict('sign_status'));

const contractInquiryList = ref<ContractInquiryVO[]>();
const loading = ref(true);
const total = ref(0);
const dateRange = ref<[DateModelType, DateModelType]>(['', '']);
const queryFormRef = ref<ElFormInstance>();
const queryParams = ref<ContractInquiryQuery>({
  pageNum: 1,
  pageSize: 10,
  startTime: '',
  endTime: '',
  opName: '',
  workName: '',
  workIdCard: '',
  workPhone: '',
  busType: '',
  signStatus: ''
});

/** 签约查询列表 */
const getList = async () => {
  loading.value = true;
  const res = await api.authList(proxy?.addDateRange(queryParams.value, dateRange.value));
  loading.value = false;
  contractInquiryList.value = res.rows;
  total.value = res.total;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  dateRange.value = ['', ''];
  queryFormRef.value?.resetFields();
  queryParams.value.pageNum = 1;
  handleQuery();
};

const dialogVisible = ref(false);
const detailForm = ref<AuthDetailVo>({
  signFileUrl: ''
});

/** 新增&修改 */
const lookDetail = async (row: any = '') => {
  detailForm.value = {
    signFileUrl: ''
  };
  dialogVisible.value = true;
  const res = await api.authDetail(row.id);
  console.log(res);
  detailForm.value = res.data;
};

onMounted(() => {
  getList(); // 初始化列表数据
});
defineExpose({
  getList
});
</script>

<style lang="scss" scoped></style>
